package batframework.mvc.render;

import java.io.File;
import java.io.InputStream;

import batframework.code.util.IoUtils;


public class FileRender extends StreamRender
{
	private  String contentType = "application/octet-stream";
	
	private String fileName;
		
	@Override
	public void render()
	{
    	response.addHeader("Content-disposition", "attachment; filename=" + fileName);
    	response.addHeader("Content-Length", ""+getLength());
    	String mimeType = servletContext.getMimeType(fileName);
    	if(mimeType != null)
    		contentType = mimeType;
    	response.setContentType(contentType);
    	super.render();
	}

	public FileRender(InputStream inputStream,String fileName)
	{
		super(inputStream);
		this.fileName = fileName;
	}

	public FileRender(File file)
	{
		this(IoUtils.getInputStream(file),file.getName());
	}
	
	public FileRender(String filePath)
	{
		this(new File(filePath));
	}
}
